home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWRgnShp.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  12.2 KB  |  469 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRgnShp.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRGNSHP_H
  13. #include "FWRgnShp.h"
  14. #endif
  15.  
  16. #ifndef SLGRGLOB_H
  17. #include "SLGrGlob.h"
  18. #endif
  19.  
  20. #ifndef FWGC_H
  21. #include "FWGC.h"
  22. #endif
  23.  
  24. #ifndef FWGRUTIL_H
  25. #include "FWGrUtil.h"
  26. #endif
  27.  
  28. #ifndef FWFXMATH_H
  29. #include "FWFxMath.h"
  30. #endif
  31.  
  32. #ifndef SLRENDER_H
  33. #include "SLRender.h"
  34. #endif
  35.  
  36. #ifndef SLREGION_H
  37. #include "SLRegion.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. #ifndef FWSOMENV_H
  45. #include "FWSOMEnv.h"
  46. #endif
  47.  
  48. #ifndef FWACQUIR_H
  49. #include "FWAcquir.h"
  50. #endif
  51.  
  52. // ----- Foundation Includes -----
  53.  
  54. #ifndef FWSTREAM_H
  55. #include "FWStream.h"
  56. #endif
  57.  
  58. #ifndef FWSOMENV_H
  59. #include "FWSOMEnv.h"
  60. #endif
  61.  
  62.  
  63. // ----- OpenDoc Includes -----
  64.  
  65. #ifndef SOM_ODTransform_xh
  66. #include <Trnsform.xh>
  67. #endif
  68.  
  69. #ifndef FW_BUILD_WIN
  70. #ifndef SOM_Module_OpenDoc_Polygon_defined
  71. #include <Polygon.xh>
  72. #endif
  73. #endif
  74.  
  75. //========================================================================================
  76. // File scope definitions
  77. //========================================================================================
  78.  
  79. #ifdef FW_BUILD_MAC
  80. #pragma segment FWGraphics_RegionShape
  81. #endif
  82.  
  83. //========================================================================================
  84. //    class FW_CRegionShape
  85. //========================================================================================
  86.  
  87. FW_DEFINE_AUTO(FW_CRegionShape)
  88. FW_DEFINE_CLASS_M1(FW_CRegionShape, FW_CShape)
  89.  
  90. // This class is archivable, but we provide the archiving implementation in a separate
  91. // translation unit in order to enable deadstripping of the archiving-related code
  92. // in parts that do not use archiving with this class.
  93.  
  94. //----------------------------------------------------------------------------------------
  95. //    FW_CRegionShape::FW_CRegionShape
  96. //----------------------------------------------------------------------------------------
  97.  
  98. FW_CRegionShape::FW_CRegionShape(ODShape* odShape,
  99.                                  FW_ERenderVerbs renderVerb,
  100.                                  const FW_CInk& ink,
  101.                                  const FW_CStyle& style) :
  102.     FW_CShape(renderVerb, ink, style, FW_kNormalFont),
  103.     fODShape(odShape)
  104. {
  105.     FW_ASSERT(odShape);
  106.     FW_SOMEnvironment ev;
  107.     fODShape->Acquire(ev);
  108.     FW_END_CONSTRUCTOR
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. //    FW_CRegionShape::FW_CRegionShape
  113. //----------------------------------------------------------------------------------------
  114.  
  115. FW_CRegionShape::FW_CRegionShape(const FW_CRegionShape& other) :
  116.     FW_CShape(other),
  117.     fODShape(NULL)
  118. {
  119.     if (other.fODShape != NULL)
  120.     {
  121.         FW_SOMEnvironment ev;
  122.         fODShape = other.fODShape->Copy(ev);
  123.     }
  124.         
  125.     FW_END_CONSTRUCTOR
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. //    FW_CRegionShape::FW_CRegionShape
  130. //----------------------------------------------------------------------------------------
  131.  
  132. FW_CRegionShape::FW_CRegionShape(FW_CReadableStream& stream) :
  133.     FW_CShape(stream),
  134.     fODShape(NULL)
  135. {
  136. #ifndef FW_BUILD_WIN
  137.     unsigned long dataSize;
  138.     stream >> dataSize;
  139.     if (dataSize != 0)
  140.     {
  141.         ODPolygon* polygon = (ODPolygon*)::operator new(dataSize);
  142.         stream.Read((char*)polygon, dataSize);
  143.         
  144.         FW_SOMEnvironment ev;
  145.         // [B1 Conversion] Should diseapear when region shape doesn't use ODShape
  146.         fODShape = new ODShape;
  147.         fODShape->InitShape(ev);
  148.         fODShape->SetPolygon(ev, polygon);
  149.     }
  150. #else
  151.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  152. #endif
  153.  
  154.     FW_END_CONSTRUCTOR
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------
  158. //    FW_CRegionShape::~FW_CRegionShape
  159. //----------------------------------------------------------------------------------------
  160.  
  161. FW_CRegionShape::~FW_CRegionShape()
  162. {
  163.     FW_START_DESTRUCTOR
  164.     FW_SOMEnvironment ev;
  165.     DisposeODShape(ev);
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. //    FW_CRegionShape::operator=
  170. //----------------------------------------------------------------------------------------
  171.  
  172. FW_CRegionShape& FW_CRegionShape::operator=(const FW_CRegionShape& other)
  173. {
  174.     if (this != &other)
  175.     {
  176.         FW_CShape::operator=(other);
  177.         
  178.         FW_SOMEnvironment ev;
  179.         DisposeODShape(ev);
  180.         fODShape = other.fODShape->Copy(ev);
  181.     }
  182.     
  183.     return *this;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. //    FW_CRegionShape::Render
  188. //----------------------------------------------------------------------------------------
  189.  
  190. void FW_CRegionShape::Render(FW_CGraphicContext& gc) const
  191. {
  192.     FW_ASSERT(fODShape != NULL);
  193.  
  194.     FW_PrivRenderRegion(gc.GetEnvironment(),
  195.                         gc,
  196.                         fODShape,
  197.                         GetRenderVerb(),
  198.                         fInk,
  199.                         fStyle);
  200.     FW_FailOnEvError(gc.GetEnvironment());
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //    FW_CRegionShape::Render
  205. //----------------------------------------------------------------------------------------
  206.  
  207. void FW_CRegionShape::RenderRegion(FW_CGraphicContext& gc,
  208.                                     ODShape* odShape, 
  209.                                     FW_ERenderVerbs renderVerb, 
  210.                                     const FW_CInk& ink,
  211.                                     const FW_CStyle& style)
  212. {
  213.     FW_ASSERT(odShape != NULL);
  214.     
  215.     FW_PrivRenderRegion(gc.GetEnvironment(),
  216.         gc,
  217.         odShape,
  218.         renderVerb,
  219.         ink,
  220.         style);
  221.     FW_FailOnEvError(gc.GetEnvironment());
  222. }
  223.  
  224. //----------------------------------------------------------------------------------------
  225. //    FW_CRegionShape::AdoptODShape
  226. //----------------------------------------------------------------------------------------
  227.  
  228. void FW_CRegionShape::AdoptODShape(ODShape* odShape)
  229. {
  230.     FW_ASSERT(odShape != NULL);
  231.     
  232.     FW_SOMEnvironment ev;
  233.     DisposeODShape(ev);
  234.     fODShape = odShape;
  235.     fODShape->Acquire(ev);
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CRegionShape::OrphanODShape
  240. //----------------------------------------------------------------------------------------
  241. // Caller will be responsible for releasing the shape
  242.  
  243. ODShape* FW_CRegionShape::OrphanODShape()
  244. {
  245.     ODShape* odTempShape = fODShape;
  246.     fODShape = NULL;
  247.     return odTempShape;
  248. }
  249.  
  250. //----------------------------------------------------------------------------------------
  251. //    FW_CRegionShape::SetGeometry
  252. //----------------------------------------------------------------------------------------
  253.  
  254. void FW_CRegionShape::SetGeometry(ODShape* odShape)
  255. {
  256.     FW_SOMEnvironment ev;
  257.     DisposeODShape(ev);
  258.     fODShape = odShape->Copy(ev);
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. //    FW_CRegionShape::DisposeODShape
  263. //----------------------------------------------------------------------------------------
  264.  
  265. void FW_CRegionShape::DisposeODShape(Environment* ev)
  266. {
  267.     if (fODShape != NULL)
  268.     {
  269.         fODShape->Release(ev);
  270.         fODShape = NULL;
  271.     }
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    FW_CRegionShape::HitTest
  276. //----------------------------------------------------------------------------------------
  277.  
  278. FW_Boolean FW_CRegionShape::HitTest(FW_CGraphicContext& gc,
  279.                                     const FW_CPoint& test,
  280.                                     FW_Fixed tolerance) const
  281. {
  282.     FW_ASSERT(fODShape != NULL);
  283.  
  284.     FW_Boolean isInside = FALSE;
  285.     
  286.     if (fRenderVerb != FW_kNoRendering)
  287.     {    
  288.         Environment* ev = gc.fEnvironment;
  289.     
  290.         ODRgnHandle rgn = ::FW_GetShapeRegion(ev, fODShape);
  291.     
  292.         FW_CRect testRect(test.x - tolerance, test.y - tolerance,
  293.                           test.x + tolerance, test.y + tolerance);
  294.         
  295.         if (::FW_RectInRegion(rgn, testRect))
  296.         {
  297.             isInside = TRUE;
  298.             if (fRenderVerb == FW_kFrame)
  299.             {
  300.                 FW_CAcquiredODShape insideShape = fODShape->Copy(ev);
  301.                 ::FW_OutlineODShape(ev, insideShape, GetPenSize());
  302.         
  303.                 ODRgnHandle insideRgn = ::FW_GetShapeRegion(ev, insideShape);
  304.                 
  305.                 isInside = !::FW_RectInRegion(insideRgn, testRect);
  306.             }
  307.         }
  308.     }
  309.     
  310.     return isInside;
  311. }
  312.  
  313. //----------------------------------------------------------------------------------------
  314. //    FW_CRegionShape::Copy
  315. //----------------------------------------------------------------------------------------
  316.  
  317. FW_CShape* FW_CRegionShape::Copy() const
  318. {
  319.     return FW_NEW(FW_CRegionShape, (*this));
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. //    FW_CRegionShape::Flatten
  324. //----------------------------------------------------------------------------------------
  325.  
  326. void FW_CRegionShape::Flatten(FW_CWritableStream& stream) const
  327. {
  328.     FW_CShape::Flatten(stream);
  329.     
  330.     if (fODShape != NULL)
  331.     {
  332. #ifdef FW_BUILD_MAC
  333.         FW_SOMEnvironment ev;
  334.         
  335.         ODPolygon* polygon = new ODPolygon;
  336.         fODShape->CopyPolygon(ev, polygon);
  337.         
  338.         ODSLong nContours = polygon->GetNContours();
  339.         unsigned long dataSize = sizeof(ODSLong) + nContours * sizeof(ODSLong);
  340.         
  341.         ODContour *c = polygon->FirstContour();
  342.         for (ODSLong i = nContours; i>0; i-- ) 
  343.         {
  344.             dataSize += c->nVertices * sizeof(ODPoint);
  345.             c = (ODContour*)&c->vertex[c->nVertices];    // give the next contour
  346.         }
  347.     
  348.         FW_TRY
  349.         {
  350.             stream << dataSize;
  351.             stream.Write((char*)polygon, dataSize);
  352.         }
  353.         FW_CATCH_BEGIN
  354.         FW_CATCH_EVERYTHING()
  355.         {
  356.             delete polygon;
  357.             FW_THROW_SAME();
  358.         }
  359.         FW_CATCH_END
  360.         
  361.         delete polygon;
  362. #else    
  363. FW_UNUSED(stream);
  364.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  365. #endif
  366.     }
  367.     else
  368.     {
  369.         unsigned long dataSize = 0;
  370.         stream << dataSize;
  371.     }
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. //    FW_CRegionShape::GetBounds
  376. //----------------------------------------------------------------------------------------
  377.  
  378. void FW_CRegionShape::GetBounds(FW_CGraphicContext& gc, FW_CRect& rect) const
  379. {
  380. FW_UNUSED(gc);
  381.     
  382.     FW_ASSERT(fODShape != NULL);
  383.     fODShape->GetBoundingBox(gc.fEnvironment, rect);
  384. }
  385.  
  386. //----------------------------------------------------------------------------------------
  387. //    FW_CRegionShape::GetAnchorPoint
  388. //----------------------------------------------------------------------------------------
  389.  
  390. FW_CPoint FW_CRegionShape::GetAnchorPoint() const
  391. {
  392.     FW_ASSERT(fODShape != NULL);
  393.  
  394.     FW_SOMEnvironment ev;
  395.     FW_CRect rect;
  396.     fODShape->GetBoundingBox(ev, rect);
  397.     
  398.     return rect.TopLeft();
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CRegionShape::Transform
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void FW_CRegionShape::Transform(Environment* ev, ODTransform* odTransform)
  406. {
  407.     FW_ASSERT(fODShape != NULL);
  408.     fODShape->Transform(ev, odTransform);
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. //    FW_CRegionShape::InverseTransform
  413. //----------------------------------------------------------------------------------------
  414.  
  415. void FW_CRegionShape::InverseTransform(Environment* ev, ODTransform* odTransform)
  416. {
  417.     FW_ASSERT(fODShape != NULL);
  418.     fODShape->InverseTransform(ev, odTransform);
  419. }
  420.  
  421. //----------------------------------------------------------------------------------------
  422. //    FW_CRegionShape::MoveShape
  423. //----------------------------------------------------------------------------------------
  424.  
  425. void FW_CRegionShape::MoveShape(FW_Fixed deltaX, FW_Fixed deltaY)
  426. {
  427.     FW_ASSERT(fODShape != NULL);
  428.  
  429.     FW_SOMEnvironment ev;
  430.         
  431.     ODTransform* odTransform = ::FW_NewODTransform(ev, FW_CPoint(deltaX, deltaY));
  432.     fODShape->Transform(ev, odTransform);
  433.     odTransform->Release(ev);
  434. }
  435.  
  436. //----------------------------------------------------------------------------------------
  437. //    FW_CRegionShape::MoveShapeTo
  438. //----------------------------------------------------------------------------------------
  439.  
  440. void FW_CRegionShape::MoveShapeTo(FW_Fixed x, FW_Fixed y)
  441. {
  442.     FW_ASSERT(fODShape != NULL);
  443.  
  444.     FW_SOMEnvironment ev;
  445.  
  446.     FW_CRect box;
  447.     fODShape->GetBoundingBox(ev, (ODRect*) &box);
  448.     MoveShape(x - box.left, y - box.top);
  449. }
  450.  
  451. //----------------------------------------------------------------------------------------
  452. //    FW_CRegionShape::Inset
  453. //----------------------------------------------------------------------------------------
  454.  
  455. void FW_CRegionShape::Inset(FW_Fixed x, FW_Fixed y)
  456. {
  457.     FW_ASSERT(fODShape != NULL);
  458.  
  459.     FW_SOMEnvironment ev;
  460.  
  461.     ODRgnHandle plfmRgn = ::FW_CopyRegion(::FW_GetShapeRegion(ev, fODShape));
  462.     
  463.     ::FW_InsetRegion(plfmRgn, x, y);
  464.  
  465.     ::FW_SetShapeRegion(ev, fODShape, plfmRgn);
  466.     ::FW_DisposeRegion(plfmRgn);
  467. }
  468.  
  469.